{include file='public/header' /}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">

            <div class="layui-form">
                <div class="layui-btn-group ">
                    <button id="btnAddRole" class="layui-btn icon-btn {:auth('business.account/add')}"><i class="layui-icon">&#xe654;</i>添加</button>
                </div>
                <div class="layui-input-inline">
                    <input type="text" name="title" id="title" placeholder="请输入课程标题" class="layui-input">
                </div>
                <button class="layui-btn" id="searchBtn"><i class="layui-icon">&#xe615;</i>搜 索</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm {:auth('business.account/edit')}" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-sm {:auth('business.account/delete')}" lay-event="del">删除</a>
                </div>
            </script>

            <script type="text/html" id="thumb">
                {{#  if(d.thumb){ }}
                <a href="{{d.thumb}}" target="_blank" title="点击查看">
                    <img src="{{d.thumb}}" alt="" width="28" height="28">
                </a>
                {{#  } }}
            </script>
            <script type="text/html" id="tableStateUser">
                <input type="checkbox" lay-filter="ckStateUser" value="{{d.id}}" lay-skin="switch"
                       lay-text="促销|正常" {{d.is_sale==1?'checked':''}}/>
            </script>
            <script type="text/html" id="tags">
                {{#  layui.each(d.tags, function(index, item){ }}
                <button type="button" class="layui-btn layui-btn-xs">{{ item.name }}</button>
                {{# }); }}
            </script>
            <script type="text/html" id="category">
                {{# if(d.category){ }}
                {{d.category.name}}
                {{# }else { }}
                分类不存在
                {{#  } }}
            </script>
        </div>
    </div>
</div>
<!-- js部分 -->
{include file='public/footer' /}
<script>
    layui.use(['layer', 'table', 'admin','form','layedit'], function (){
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var table = layui.table;
        var layedit = layui.layedit;
        //用户表格初始化
        var dataTable = table.render({
            elem: '#dataTable'
            , autoSort: false
            , height: 500
            , url: "{:__url('business.account/getData')}" //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {checkbox: true, fixed: true}
                , {field: 'id', title: 'ID', sort: true, width: 50}
                , {field: 'thumb', title: '主图',align: 'center', toolbar: '#thumb', width: 100}
                , {field: 'title', title: '标题',width:150}
                , {field: 'category', title: '分类', toolbar: '#category'}

                /* , {field: 'keywords', title: '关键词'}*/
                /*  , {field: 'tags', title: '标签', toolbar: '#tags', width: 150}*/
                , {field: 'price', title: '单价'}
                , {field: 'more_price', title: '团购价'}
                ,{
                    title: '状态', templet: function (d) {
                        var strs = ['<span class="layui-badge layui-bg-blue">待审核</span>','<span class="layui-badge layui-bg-green">上架</span>', '<span class="layui-badge layui-bg-red">下架</span>'];
                        return strs[d.status];
                    }, align: 'center'
                }
                /*, {field: 'created_at', title: '创建时间'}*/
                , {field: 'update_time', title: '更新时间'}
                ,{field: 'is_sale', sort: true, templet: '#tableStateUser', title: '促销操作',width:110}
                , {fixed: 'right', width: 140, align: 'center', toolbar: '#options', title: '操作'},

            ]]
        });


        //监听工具条
        table.on('tool(dataTable)', function (obj) { //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del'){
                layer.confirm('确认删除吗？',function (index){
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:__url('business.account/delete')}", {
                        _method: 'delete',
                        id: [data.id]
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1,time:1000}, function () {
                                obj.del();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000})
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                showEditModel('编辑课程','/admin/business.account/edit?id=' + data.id);

            }

        });
        //
        form.on('switch(ckStateUser)', function (obj) {
            layer.load(2);
            $.post("{:__url('business.account/action')}", {
                id: obj.elem.value,
                is_sale: obj.elem.checked ? 1 : 0
            }, function (res) {
                layer.closeAll('loading');
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1,time:1000});
                } else {
                    layer.msg(res.msg, {icon: 2,time:1000});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });
        //按钮批量删除
        $("#listDelete").click(function () {
            var ids = [];
            var hasCheck = table.checkStatus('dataTable');
            var hasCheckData = hasCheck.data;
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    ids.push(element.id)
                })
            }
            if (ids.length > 0) {
                layer.confirm('确认删除吗？', function (index) {
                    layer.close(index);
                    var load = layer.load();
                    $.post("{:__url('business.account/action')}", {
                        _method: 'delete',
                        ids: ids
                    }, function (res) {
                        layer.close(load);
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1,time:1000}, function () {
                                dataTable.reload({page: {curr: 1}});
                                //dataTable();
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000})
                        }
                    });
                });
            } else {
                layer.msg('请选择删除项', {icon: 2,time:1000})
            }
        })

        // 添加按钮点击事件
        $('#btnAddRole').click(function (){
            showEditModel('新增课程',"{:__url('business.account/add')}");
        });

        // 显示表单弹窗
        function showEditModel(title,url) {
            layer.full(admin.open({
                type: 2,

                title: title,
                content: url,
                btn:['提交','取消'],
                btnAlign: 'c',   // 按钮居中
                success: function (layero, dIndex) {
                    form.render();    // 表单渲染
                },

                yes: function (index, layero) {

                    // 获取弹出层中的form表单元素
                    var formSubmit=layer.getChildFrame('form', index);

                    //获取表单数据
                    var data = {dosubmit:1};
                    var action = formSubmit[0]['action'];
                    var a = formSubmit.serializeArray();

                    $.each(a, function () {
                        if (data[this.name] !== undefined) {
                            if (!data[this.name].push) {
                                data[this.name] = [data[this.name]];
                            }
                            data[this.name].push(this.value || '');
                        } else {
                            data[this.name] = this.value || '';
                        }
                    });

                    if (data.parent_id == '') {
                        data.parent_id = '0';
                    }

                    layer.load(2);
                    $.post(action, data, function (res) {
                        layer.closeAll('loading');
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1,time:1000});
                            layer.close(index);

                            dataTable.reload({page: {curr: 1}});

                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    }, 'json');

                    return false;

                },
                btn2: function (index, layero) {
                    layer.close(index);
                }


            }));
        }



        //搜索
        $("#searchBtn").click(function () {
            var catId = $("#category_id").val();
            var title = $("#title").val();
            dataTable.reload({
                where: {category_id: catId, title: title},
                page: {curr: 1}
            })
        })
    })
</script>

</body>
</html>
